package cn.edu.cdu.farmdeal.service.impl;

import cn.edu.cdu.farmdeal.domain.*;
import cn.edu.cdu.farmdeal.mapper.*;
import cn.edu.cdu.farmdeal.service.OrderService;
import cn.edu.cdu.farmdeal.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: tupeng
 * @Date: 2020/4/1
 **/
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private AddressMapper addressMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserInfMapper userInfMapper;
    @Autowired
    private UserProductMapper userProductMapper;


    @Override
    public int confirmReceipt(int id) {
        return orderMapper.confirmReceipt(id);
    }

    @Override
    public List<UserProduct> getUserPro() {
        return userProductMapper.getUserPro();
    }

    @Override
    public Order saveOrderInfo(Map<String, Object> map) throws ClassCastException{
        Integer id = (Integer) map.get("product_id");
        Integer buySum = (Integer) map.get("buy_sum");
        Order order = new Order();
        order.setOrderId((int) (map.get("order_id")));
        order.setCreateTime(new Date());
        order.setBuySum(buySum);
        order.setPriceSum(new BigDecimal(String.valueOf(map.get("price_sum"))) );
        order.setLeaveMessage((String) map.get("leave_message"));
        order.setUserId((String) map.get("user_id"));
        //根据产品Id查shopID
        order.setShopId(productMapper.getShopId(id));
        //插入订单
        order.setProductName((String)map.get("product_name"));
        order.setProductId((Integer) map.get("product_id"));
        return order;
    }

    @Override
    public int getTotalCount(String username, Order order, Address address) {
        return orderMapper.getTotalCount(username, order, address);
    }

    @Override
    public List<Map<String, Object>> getOrderInfo(String name, Page page, Order order, Address address) {
        List<Map<String, Object>> maps = new ArrayList<Map<String, Object>>();
        List<Map<String, Object>> mapList = orderMapper.getOrderInfo(name, page, order, address);
        for (Map<String, Object> map : mapList) {
            String userAddress = (String) map.get("region") + " " + (String) map.get("street");
            map.put("userAddress", userAddress);
            maps.add(map);
        }
        return maps;
    }




    @Override
    public Boolean checkAddress(String userid) {
        Address addresses = addressMapper.checkAddress(userid);
        return  addresses== null ? false : true;
    }

    @Override
    public int updateOrderStatusPay(int orderNo) {
        return orderMapper.updateOrderStatusPay(orderNo);
    }

    @Override
    public int saveUserPro(String userid, int proid) {
        UserProduct userProduct = new UserProduct();
        userProduct.setProductId(proid);
        userProduct.setUserInfoId(userInfMapper.getUserNameByName(userid).getUserInfoId());
        return userProductMapper.insertSelective(userProduct);
    }

    @Override
    public void doStock(int miaoshaGoodsId, int buySum) {
        orderMapper.updateStock(miaoshaGoodsId, buySum);
    }

    @Override
    public void addToOrder(Order order) {
        orderMapper.insertOrder(order);
    }

    @Override
    public Order getOrderInfoByOrderNo(int orderNo) {
        return null;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Order doOrderInfo(Map<String, Object> map) {
        String id = (String) map.get("product_id");
        String buySum = (String) map.get("buy_sum");
//        doStock(id, buySum);
        Order order = new Order();
        Long orderNo = System.currentTimeMillis();
        String orderno = String.valueOf(orderNo).substring(1,5);
        order.setOrderId(Integer.parseInt(orderno+ id));
        order.setCreateTime(new Date());
        order.setBuySum(Integer.parseInt(buySum));
        order.setPriceSum(new BigDecimal((String) map.get("total_price")));
        order.setLeaveMessage((String) map.get("leave_message"));
        order.setUserId((String) map.get("user_id"));
        //根据产品Id查shopID
        order.setShopId(productMapper.getShopId(Integer.parseInt(id)));
        //插入订单
        order.setProductId(Integer.parseInt(id));
        System.out.println(order);
        //根据产品ID查询产品信息
        Product product = productMapper.getProById(Integer.parseInt(id));
        order.setProductName(product.getName());
        order.setProductImg(product.getProductImg());
        order.setPrice(new BigDecimal(String.valueOf(product.getNewPrice())));
        order.setStatus(0);
        addToOrder(order);
        return order;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void doStocks(Order order) {
        doStock( order.getProductId(),order.getBuySum());
    }

    @Override
    public CartShopPro getProById(String id, String buySum) {
        CartShopPro cartShopPro = new CartShopPro();
        Product product = productMapper.getProById(Integer.parseInt(id));
        cartShopPro.setBuySum(Integer.parseInt(buySum));
        cartShopPro.setPrice(product.getNewPrice());
        cartShopPro.setProductName(product.getName());
        cartShopPro.setProductImg(product.getProductImg());
        cartShopPro.setSumPrice(product.getNewPrice().multiply(BigDecimal.valueOf(Integer.parseInt(buySum))));
        return cartShopPro;
    }

    @Override
    public int saveAddress(Map<String, Object> map) {
        String consignee = (String) map.get("consignee");
        String tag = (String) map.get("tag");
        String tel = (String) map.get("tel");
        String region = (String) map.get("region");
        String street = (String) map.get("street");
        String pay_way = (String) map.get("pay_way");
        String delivery_way = (String) map.get("delivery_way");
        String userName = (String) map.get("userId");
        Address address = new Address();
        address.setConsignee(consignee);
        address.setRegion(region);
        address.setDeliveryWay(delivery_way);
        address.setPayWay(pay_way);
        address.setTag(tag);
        address.setStreet(street);
        address.setUserId(userName);
        address.setTel(tel);
        address.setDeliveryTime("默认");
        return addressMapper.insertAdd(address);
    }

    @Override
    public int updateAddress(Map<String, Object> map) {
        String consignee = (String) map.get("consignee");
        String tag = (String) map.get("tag");
        String tel = (String) map.get("tel");
        String region = (String) map.get("region");
        String street = (String) map.get("street");
        String pay_way = (String) map.get("pay_way");
        String delivery_way = (String) map.get("delivery_way");
        String userName = (String) map.get("userName");
        Address address = new Address();
        address.setConsignee(consignee);
        address.setRegion(region);
        address.setDeliveryWay(delivery_way);
        address.setPayWay(pay_way);
        address.setTag(tag);
        address.setStreet(street);
        address.setUserId(userName);
        address.setTel(tel);
        address.setDeliveryTime("默认");
        AddressExample addressExample = new AddressExample();
        addressExample.createCriteria().andUserIdEqualTo(userName);
        return addressMapper.updateByExampleSelective(address, addressExample);
    }
}
